/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Trazar.java
 *
 * Created on 30/05/2011, 16:48:59
 */
package vistas;

import Renderers.CellRenderer;
import comu.Utilitats;
import controlador.ProduccioControlador;
import entity.Collita;
import entity.Produccio;
import java.lang.String;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public class Trazar extends javax.swing.JFrame {

    Menu menu;
    ProduccioControlador produccions;

    /** Creates new form Trazar */
    public Trazar(Menu menu) {
        this.menu = menu;
        produccions = new ProduccioControlador();
        initComponents();
        Utilitats.centrarVentana(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDescripcio = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        treeCollites = new javax.swing.JTree();
        jScrollPane3 = new javax.swing.JScrollPane();
        taCollita = new javax.swing.JTextArea();
        btMenu = new javax.swing.JButton();
        lbInfo = new javax.swing.JLabel();
        ftfCodi = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText(Textos.TRAZAR_LABEL_CODI);

        btBuscar.setText(Textos.TRAZAR_BOTO_TRAZAR);
        btBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBuscarActionPerformed(evt);
            }
        });

        taDescripcio.setColumns(20);
        taDescripcio.setEditable(false);
        taDescripcio.setFont(Textos.FONT_GRAN);
        taDescripcio.setRows(5);
        jScrollPane1.setViewportView(taDescripcio);

        treeCollites.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
        treeCollites.setCellRenderer(new CellRenderer());
        treeCollites.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeCollitesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(treeCollites);

        taCollita.setColumns(20);
        taCollita.setFont(Textos.FONT_GRAN);
        taCollita.setRows(5);
        jScrollPane3.setViewportView(taCollita);

        btMenu.setText(Textos.COMU_BOTO_MENU);
        btMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenuActionPerformed(evt);
            }
        });

        lbInfo.setText(Textos.COMU_INFO_INFO);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 302, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(lbInfo)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 105, Short.MAX_VALUE)
                                .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(ftfCodi, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btBuscar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(btBuscar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ftfCodi, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 246, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lbInfo)))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenuActionPerformed
        if (menu != null) {
            menu.setVisible(true);
            this.setVisible(false);
        }
}//GEN-LAST:event_btMenuActionPerformed

    private void btBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBuscarActionPerformed
        taDescripcio.setText("");
        taCollita.setText("");
        treeCollites.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));;
        String input = ftfCodi.getText();
        if ((input != null) && (input.length() == 10)) {
            try {
                String produccioId = input.substring(0, 4);
                Integer id = Integer.parseInt(produccioId);
                Produccio produccio = produccions.findProduccio(id);
                if (produccio != null) {
                    String descrip = "";
                    descrip += Textos.TRAZAR_TEXT_PRODCUTOR + produccio.getCampId().getProductorId().getNom() + Textos.COMU_ENTER + Textos.COMU_ENTER;
                    descrip += Textos.TRAZAR_TEXT_CAMP + produccio.getCampId().getNom() + Textos.COMU_ENTER;
                    descrip += Textos.TRAZAR_TEXT_LOCALITAT + produccio.getCampId().getLocalitat() + Textos.COMU_ENTER + Textos.COMU_ENTER;
                    descrip += Utilitats.getDescripcio(produccio);
                    taDescripcio.setText(descrip);
                    treeCollites.setModel(Utilitats.getTreeModel(produccio));
                } else {
                    JOptionPane.showMessageDialog(this, Textos.TRAZAR_INFO_PRODUCCIO_NO_TROVADA, "", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, Textos.TRAZAR_INFO_CODI_NO_VALID, "", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, Textos.TRAZAR_INFO_CODI_NO_VALID, "", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btBuscarActionPerformed

    private void treeCollitesValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeCollitesValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeCollites.getLastSelectedPathComponent();
        if (node != null) {
            Object value = node.getUserObject();
            String out = "";
            if (Collita.class.isInstance(value)) {
                Collita collita = (Collita) value;
                out = Utilitats.getDescripcio(collita);
            }
            taCollita.setText(out);
        }
    }//GEN-LAST:event_treeCollitesValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBuscar;
    private javax.swing.JButton btMenu;
    private javax.swing.JTextField ftfCodi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbInfo;
    private javax.swing.JTextArea taCollita;
    private javax.swing.JTextArea taDescripcio;
    private javax.swing.JTree treeCollites;
    // End of variables declaration//GEN-END:variables
}
